<template>
  <a-modal
    title="新建用户"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    okText="确认"
    cancelText="取消"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-card>
            <person-select @getPerson="onSelectPerson"></person-select>
          </a-card>
          <a-divider />
          <a-row :gutter="12">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="用户姓名"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <span v-if=" persons !== undefined ">
                  {{ this.persons.personName }}
                </span>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="用户部别"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <span v-if=" persons !== undefined ">
                  {{ this.userUnitName }}
                </span>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="12">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="登录账号"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <a-input
                  v-decorator="['loginName', {rules: [
                    { required: true, message: '请输入登录账号', trigger: 'blur' },
                    { type: 'string', min: 2, message: '登录账号不允许小于2位', trigger: 'blur' }]}]" />
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="备注信息"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <a-input v-decorator="['userInfo']" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="12">
            <a-col :md="12" :sm="24">
              <a-form-item
                label="用户角色"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <a-select v-decorator="['roleId', {rules: [{required: true, message: '请选择角色'}]}]" placeholder="请选择">
                  <a-select-option v-for="role in roles" :key="role.id">{{ role.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="数据权限"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
              >
                <a-tree-select
                  v-decorator="['unitId', {initialValue: this.persons.unitId, rules: [{required: true, message: '请选择部别'}]}]"
                  style="width: 100%"
                  :tree-data="treeData"
                  :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                  placeholder="请选择"
                  allow-clear
                  :treeDefaultExpandedKeys="expandedKeys"
                />
              </a-form-item>
            </a-col>
          </a-row>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { addUser, validateLoginName } from '@/api/basic/user'
import { listUnit, listUnitTree } from '@/api/basic/unit'
import { TreeSelect } from 'ant-design-vue'
import PersonSelect from '@/components/PersonSelect/PersonSelect'

export default {
  name: 'AddUser',
  components: {
    ATreeSelect: TreeSelect,
    PersonSelect
  },
  props: {
    roles: {
      type: Array,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      treeData: [],
      expandedKeys: [1],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      persons: [],
      userUnitName: '',
      units: [],

      form: this.$form.createForm(this)
    }
  },
  created () {
    listUnitTree()
      .then(res => {
        if (res.message !== 'error') {
          this.treeData = res.result
        } else {
          this.$message.error('操作失败！')
        }
      })
    listUnit()
      .then(res => {
        if (res.message !== 'error') {
          this.units = res.result
        } else {
          this.$message.error('操作失败！')
        }
      })
  },
  methods: {
    onSelectPerson (persons) {
      this.persons = persons
      for (const unit of this.units) {
        if (unit.unitId === this.persons.unitId) {
          this.userUnitName = unit.unitName
          break
        }
      }
      console.log(persons)
    },
    add () {
      this.visible = true
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          validateLoginName({ 'loginName': values.loginName })
            .then(res => {
              if (res.message !== 'error') {
                addUser(Object.assign(values, {
                  'userName': this.persons.personName,
                  'unitName': this.userUnitName,
                  'userType': 1 }))
                  .then(res => {
                    if (res.message !== 'error') {
                      this.visible = false
                      this.confirmLoading = false
                      this.form.resetFields()
                      this.$emit('ok')
                    } else {
                      this.confirmLoading = false
                      this.$message.error('操作失败！')
                    }
                  })
              } else {
                this.confirmLoading = false
                this.$message.error('登录账号已经存在！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
